﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StirringRod : BaseBrokenObj
{

    float distanceTop;
    float distanceDown;
    float distanceLeft;
    float distanceRight;

    public override void OnInit()
    {
        base.OnInit();

    }

    public override void OnHoldingTrigger(Ray newRay)
    {
        base.OnHoldingTrigger(newRay);
        if (mUpTrasnform != null)
            distanceTop = mUpTrasnform.position.y;
        if (mDownTrasnform != null)
            distanceDown = mDownTrasnform.position.y;
        if (mLeftTrasnform != null)
            distanceLeft = mLeftTrasnform.position.x;
        if (mRightTrasnform != null)
            distanceRight = mRightTrasnform.position.x;
        if (m_InFunnel)
        {
            Ray deviceRay = newRay;
            Vector3 hitPos;
            if (RayTriInsecPos(m_IntersectPlane[0], m_IntersectPlane[1], m_IntersectPlane[2], deviceRay, out hitPos))
            {
                transform.position = hitPos;
                if (transform.position.y <= distanceDown)
                {
                    transform.position = new Vector3(transform.position.x, distanceDown, transform.position.z);
                }
                if (transform.position.x >= distanceRight)
                {
                    transform.position = new Vector3(distanceRight, transform.position.y, transform.position.z);
                }
                if (transform.position.x <= distanceLeft)
                {
                    transform.position = new Vector3(distanceLeft, transform.position.y, transform.position.z);
                }
            }
        }
    }

    bool m_InFunnel;
    int ClearNum;
    private void OnTriggerEnter(Collider other)
    {
        if (other.name == "Funnel")
        {
            m_InFunnel = true;
            if (StepsManager.Instance.StepTwoNum[2])
            {
                StepsManager.Instance.StepTwoNum[3] = true;
                StepsManager.Instance.SetStepTwo(3);
            }
        }

        if (other.name == "BeakerBrush")
        {
            if (StepsManager.Instance.StepFourNum[1])
            {
                if (!StepsManager.Instance.StepFiveNum[0])
                {
                    ClearNum++;
                    if (ClearNum == 2)
                    {
                        StepsManager.Instance.StepFiveNum[0] = true;
                        StepsManager.Instance.SetStepFive(0);
                    }
                }
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.name == "Funnel")
        {
            m_InFunnel = false;
        }
    }
}
